﻿package com.toncen.samepms.net.action;

import java.util.ArrayList;
import java.util.List;

import com.toncen.samepms.common.AbstractDatabaseAction;
import com.toncen.samepms.interceptor.In;
import com.toncen.samepms.interceptor.Out;
import com.toncen.samepms.net.dao.ChannelDao;
import com.toncen.samepms.net.domain.Channel;
import com.toncen.samepms.net.domain.RoleChannel;
import com.toncen.samepms.system.dao.RoleDao;
import com.toncen.samepms.system.domain.Role;

/**
 * 描述：栏目表NET_CHANNEL的action文件
 * 
 * @author xyp
 * @time 2013-5-10
 */
public class ChannelAction extends AbstractDatabaseAction<Channel, ChannelDao> {
	/**
	 * 序列号
	 */
	private static final long serialVersionUID = 1365474907286L;

	public ChannelAction() throws Exception {
		super();
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.toncen.samepms.common.AbstractAction#prepareOnDoEdit()
	 */
	@Override
	@SuppressWarnings("unchecked")
	public void prepareOnDoEdit() throws Exception {
		super.prepareOnDoEdit();
		iniPublic();
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.toncen.samepms.common.AbstractAction#prepareOnDoNew()
	 */
	@Override
	public void prepareOnDoNew() throws Exception {
		super.prepareOnDoNew();
		iniPublic();
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.toncen.samepms.common.AbstractAction#prepareOnDoView()
	 */
	@Override
	public void prepareOnDoView() throws Exception {
		super.prepareOnDoView();
		iniPublic();

	}

	/**
	 * 保存之前初始化角色列表
	 * 
	 * @return the sumTime
	 */
	private void iniPublic() throws Exception {
		getDomain().setParent(getDao().find(getDomain().getParent()));
		setRoleList(new RoleDao().getAllByState(1));
		List<RoleChannel> roleChannels = getDomain().getRoleChannels();
		Long[] roles = new Long[roleChannels.size()];
		for (int i = 0; i < roleChannels.size(); i++) { // 循环栏目角色
			roles[i] = roleChannels.get(i).getRole().getId();
		}
		setRolesChecked(roles);
	}

	/**
	 * 保存之前初始化角色列表
	 * 
	 * @return the sumTime
	 */
	@Override
	public void prepareOnDoSave() throws Exception {
		super.prepareOnDoSave();
		RoleChannel roleChannel;
		Role role;
		getDomain().getRoleChannels().clear();
		for (long roleId : rolesChecked) { // 循环所选角色
			roleChannel = new RoleChannel();
			roleChannel.setChannel(getDomain());
			role = new Role();
			role.setId(roleId);
			roleChannel.setRole(role);
			getDomain().getRoleChannels().add(roleChannel);
		}

	}

	/**
	 * 可以选择的角色列表
	 */
	@In
	@Out
	private List<Role> roleList = new ArrayList<Role>();

	/**
	 * 选中的多个角色
	 */
	@In
	@Out
	private Long[] rolesChecked;

	public List<Role> getRoleList() {
		return roleList;
	}

	public void setRoleList(List<Role> roleList) {
		this.roleList = roleList;
	}

	public Long[] getRolesChecked() {
		return rolesChecked;
	}

	public void setRolesChecked(Long[] rolesChecked) {
		this.rolesChecked = rolesChecked;
	}

}
